<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\Log\Logger;

$entry = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
?>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type='.$objectType));?>">
    <?php
    if ($entry != null) {
    ?>
    <input
        type="hidden"
        name="id"
        id="entry_id"
        value="<?php Printer::display($entry->getId()); ?>" />
        
    <input
        type="button"
        value="Envoyer les infos de la fiche aux sites"
        onclick="javascript:sendEntryInfo()" />
    <span id="importMessage"></span>
    <?php
    }
    ?>

    <div class="label">
        <span class="titre">Utilisateur :</span>
        <?php
        $userId = '' ;
        if (!is_null($entry) && !is_null($entry->getUser())) {
            $userId = $entry->getUser()->getId();
        ?>
        <span class="value">
            <?php Printer::display($entry->getUser()->toString()); ?>
        </span>
        <?php
        } else {
        ?>
        <input class="firstInput userSelecter" type="text" />
        <?php 
        }
        ?>
        <input
            type="hidden"
            id="userSelecterId"
            name="user_id"
            value="<?php Printer::display($userId); ?>" />
    </div>

    <div class="label">
        <span class="titre">Catégorie :</span>
        <?php
        $categoryId = '' ;
        if (!is_null($entry) && !is_null($entry->getCategory())) {
            $categoryId = $entry->getCategory()->getId();
        ?>
        <span class="value">
            <?php Printer::display($entry->getCategory()->toString()); ?>
        </span>
        <?php
        } else {
        ?>
        <input class="firstInput categorySelecter" type="text" />
        <?php 
        }
        ?>
        <input
            type="hidden"
            id="categorySelecterId"
            name="category_id"
            value="<?php Printer::display($categoryId); ?>" />
    </div>

    <div class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="name"
            value="<?php if ($entry != null) Printer::display($entry->getName()); ?>" />
    </div>

    <div class="label">
        <span class="titre">Adresse :</span>
        <input
            class="firstInput"
            type="text"
            name="address"
            value="<?php if ($entry != null) Printer::display($entry->getAddress()); ?>" />
    </div>

    <div class="label">
        <span class="titre">Ville :</span>
        <input
            class="firstInput"
            type="text"
            name="city"
            value="<?php if ($entry != null) Printer::display($entry->getCity()); ?>" />
    </div>

    <div class="label">
        <span class="titre">Code postal :</span>
        <input
            class="firstInput"
            type="text"
            name="zip"
            value="<?php if ($entry != null) Printer::display($entry->getZip()); ?>" />
    </div>

    <div class="label">
        <span class="titre">Téléphone :</span>
        <input
            class="firstInput"
            type="text"
            name="tel"
            value="<?php if ($entry != null) Printer::display($entry->getTel()); ?>" />
    </div>

    <div class="label oblig">
        <span class="titre">Sites : </span>
        <div class="formPartsOrderer" id="sites">
            <ul class="formPartsList">
                <?php 
                if ($entry != null) {
                    $logger = new Logger("profile");
                    $sites = $entry->getSites();

                    if (!is_null($sites)) {
                        for ($i=0; $i<count($sites); $i++) {
                            $part = $sites[$i] ;
                            $site = $part->getSite();
                            if (!is_null($site)) {
                ?>
                <li>
                    <span class="item">
                        <?php Printer::display($site->getName()); ?> - 
                        <input
                            class="id"
                            type="hidden"
                            name="site_part_id[]"
                            value="<?php Printer::display($site->getId()); ?>" />
                        <label>
                            Id sur le site : 
                            <input
                                type="text"
                                name="site_id_for_site[]"
                                size="2"
                                value="<?php Printer::display($part->getSiteUserId()); ?>" />
                        </label>
                        <input
                            class="order"
                            type="hidden"
                            name="site_part_order[]"
                            value="<?php Printer::display($i+1); ?>" />
                    </span>
                </li>
                <?php 
                            }
                        }
                    }
                }
                ?>
            </ul>
        </div>
    </div>

    <input type="submit" value="OK" />
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#sites .formPartsList").formPartsOrderer(
            {
                "serviceUrl":"?service=ListSites&format=json",
                "container":"#sites",
                "prefix":"site",
                "fields":[
                    {
                        "type":"text",
                        "name":"id_for_site",
                        "label":"Id sur le site",
                        "size":"2"
                    }
                ]
            }
        );

        $(".categorySelecter").autocomplete({
            source: "?service=ListCategories&format=json",
            select: function(event, ui) {
                $(".categorySelecter").val(ui.item.label);
                $("#categorySelecterId").val(ui.item.id);
            }
        });

        $(".userSelecter").autocomplete({
            source: "?service=ListUsers&format=json",
            select: function(event, ui) {
                $(".userSelecter").val(ui.item.label);
                $("#userSelecterId").val(ui.item.id);
            }
        });
    });

    function sendEntryInfo() {
        var entry = $("#entry_id").val();
        if (entry == undefined) {
             alert('Identifiant non valide !');
        }
        else{
            var url = "?service=SendEntryUpdate&entry_id=" + entry ;
            $.ajax({
                url: url,
                type: 'GET',
                cache: false,
                timeout: 60000,
                beforeSend: function() {
                    $('#importMessage').html('En cours d\'envoi...')
                },
                complete: function(data) {
                    $('#importMessage').html('');
                    alert(data.responseText);
                }
            });
        }
    }
</script>
